var express = require('express');
var router = express.Router();
var booksDAO = require("../bin/booksDAO");
var bouDAO = require("../bin/boutiqueDAO");
var commentDAO = require("../bin/commentDAO");
var repliesDAO = require("../bin/replyDAO");

// 获取轮播图
router.get("/lunbo",function(req,res){
  var pic = [
    {img:"/api/images/pic/wap424.jpg"},
    {img:"/api/images/pic/wap443.jpg"},
    {img:"/api/images/pic/wap448.jpg"}
  ];
  var text = [
    "坚决贯彻落实“扫黄打非”专项行动，让用户多读好书！",
    "读来读往，更多畅读体验尽在咪咕阅读专区。",
    "下载客户端，离线看小说，签到送书海币。"
  ]
  res.json({err:0,data:{pic,text}})
})

// 查询所有书籍
var pageSize = 6;
router.post("/bookslist",(req,res)=>{
  var category = ["全部","古言","现言","幻情","同人","穿越","青春","灵异","纯爱"];
  var number = ["全部","30万以下","30万-50万","50万-100万","100万-200万","200万以上"];
  var state = ["全部","连载中","完结"];
  booksDAO.find(req.body.find)
  .sort(req.body.sort)
  .skip(pageSize*req.body.page)
  .limit(pageSize)
  .then(data=>{
    res.json({err:0,data:data,category:category,number:number,state:state});
  })
})

// 按id查找数据
router.get("/booksId",(req,res)=>{
  booksDAO.findOne({_id:req.query.id})
  .populate({
    path:"comments",
    populate:{
      path:"replies"
    }
  })
  .then(data=>{
    for(var i = 0;i<data.comments.length;i++){
      data.comments[i].time = getLocalTime(data.comments[i].time);
      for(var j = 0;j<data.comments[i].replies.length;j++){
        data.comments[i].replies[j].time = getLocalTime(data.comments[i].replies[j].time);
      }
    }
    res.json({err:0,data:data});
  })
})

// 查找热搜
router.get("/hot",(req,res)=>{
  req.query.num = req.query.num*1;
  booksDAO.find().sort({read:-1}).limit(req.query.num).then(data=>{
    res.json({err:0,data:data});
  })
})

// 本周主打
router.get("/zhu",(req,res)=>{
  booksDAO.find().sort({read:-1})
  .skip(req.query.page*2)
  .limit(2)
  .then(data=>{
    res.json({err:0,data:data});
  })
})

// 完本推荐
router.get("/wan",(req,res)=>{
  booksDAO.find({state:"完结"}).sort({read:-1}).limit(4).then(data=>{
    res.json({err:0,data:data});
  })
})
// 新书
router.get("/xin",(req,res)=>{
  booksDAO.find({state:"连载中"}).sort({number:1}).limit(4).then(data=>{
    res.json({err:0,data:data});
  })
})

// 搜索查询
router.get("/sou",(req,res)=>{
  var reg = new RegExp(req.query.search);
  booksDAO.find({"$or":[{"name":reg},{"user":reg},{"category":reg},{"introduction":reg},]}).then(data=>{
    res.json({err:0,data:data});
  })
})

// 免费书籍
router.get("/free",(req,res)=>{
  booksDAO.find({isfree:"免费"}).sort({read:-1}).limit(12).then(data=>{
    res.json({err:0,data:data})
  })
})

// 添加书籍
router.post("/addbooks",(req,res)=>{
  var b = new booksDAO(req.body);
  b.save().then(data=>{
    res.json({err:0,msg:"添加成功"})
  })
})
// 添加精品
router.post("/addbou",(req,res)=>{
  var b = new bouDAO(req.body);
  b.save().then(data=>{
    res.json({err:0,msg:"添加成功"})
  })
})
// 查询精品
router.get("/getbou",(req,res)=>{
  bouDAO.find().then(data=>{
    res.json({err:0,data:data});
  })
})
// 查询某个精品
router.get("/getBouDetails",(req,res)=>{
  bouDAO.findOne({_id:req.query.id}).populate("id").then(data=>{
     res.json({err:0,data:data});
  })
})

//时间戳转日期 
function getLocalTime(nS) {  
 return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)
};
// var a = Date.parse(new Date())/1000;

// 添加书评
router.post("/comment",(req,res)=>{
  var c = new commentDAO({
    time:Date.parse(new Date())/1000,
    userName:req.body.userName,
    userAvatar:req.body.userAvatar,
    text:req.body.text
  });
  c.save().then(data1=>{
    booksDAO.findOne({_id:req.body.bookId}).then(data2=>{
      data2.comments.push(data1._id);
      data2.save().then(data=>{
        data1.time = getLocalTime(data1.time);
        res.json({err:0,data:data1,msg:"评论成功"});
      })
    })
  })

})

// 添书评加回复
router.post("/replies",(req,res)=>{
  var r = new repliesDAO({
    time:Date.parse(new Date())/1000,
    userName:req.body.userName,
    userAvatar:req.body.userAvatar,
    text:req.body.text
  });
  r.save().then(data1=>{
    commentDAO.findOne({_id:req.body.commentID}).then(data2=>{
      data2.replies.push(data1._id);
      data2.save().then(data=>{
        data1.time = getLocalTime(data1.time);
        res.json({err:0,data:data1,msg:"评论成功"});
      })
    })
  })

})


module.exports = router;
